package com.guigu.wenzhiyi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.liuyunan.mapper.OrderMapper;
import com.guigu.liuyunan.mapper.SetmealMapper;
import com.guigu.liuyunan.pojo.Order;
import com.guigu.liuyunan.pojo.Setmeal;
import com.guigu.tankai.mapper.MemberMapper;
import com.guigu.tankai.mapper.MemberexaminationMapper;
import com.guigu.tankai.mapper.MembermedicalhistoryMapper;
import com.guigu.tankai.mapper.UserMapper;
import com.guigu.tankai.pojo.Member;
import com.guigu.tankai.pojo.Memberexamination;
import com.guigu.tankai.pojo.Membermedicalhistory;
import com.guigu.tankai.pojo.User;
import com.guigu.wenzhiyi.mapper.BodytypeMapper;
import com.guigu.wenzhiyi.mapper.InterposeclassifyMapper;
import com.guigu.wenzhiyi.mapper.MentalityresultMapper;
import com.guigu.wenzhiyi.pojo.Bodytype;
import com.guigu.wenzhiyi.pojo.Interposeclassify;
import com.guigu.wenzhiyi.pojo.Mentalityresult;
import com.guigu.wenzhiyi.service.InterposeclassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InterposeclassifyServiceImpl  implements InterposeclassifyService {

    @Autowired
    InterposeclassifyMapper interposeclassifyMapper;
    @Autowired
    MembermedicalhistoryMapper membermedicalhistoryMapper;
    @Autowired
    BodytypeMapper bodytypeMapper;
    @Autowired
    MentalityresultMapper mentalityresultMapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    MemberexaminationMapper memberexaminationMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public PageInfo<Member> page(Integer num, Integer size, String name) {
        PageHelper.startPage(num,size);
        List<Member> list = memberMapper.Interposeclassifycha(name);
        for (Member member : list) {
            Interposeclassify interposeclassify = interposeclassifyMapper.cha(member.getId());
            if (interposeclassify!=null){
                member.setInterposeclassify(interposeclassify);
                if (member.getHiduser()!=null){
                    User user = userMapper.selectByPrimaryKey(member.getHiduser());
                    if (user!=null){
                        member.setUser(user);
                    }
                }
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    public int add(Interposeclassify interposeclassify) {
        interposeclassifyMapper.delete(interposeclassify.getMemberid());
        int i = interposeclassifyMapper.insert(interposeclassify);
        Member member = new Member();
        member.setId(interposeclassify.getMemberid());
        member.setJgzt(1);
        member.setHiduser(interposeclassify.getHealthmgrid());
        memberMapper.upH_ID(member);
        memberMapper.zt(member);
        return i;
    }

    @Override
    public Membermedicalhistory Memberaddcha(String id) {
        return membermedicalhistoryMapper.addcha(id);
    }

    @Override
    public List<Bodytype> Bodytypeaddcha(String id) {
        return bodytypeMapper.addcha(id);
    }

    @Override
    public List<Mentalityresult> Mentalityresultaddcha(String id) {
        return mentalityresultMapper.addcha(id);
    }

    @Override
    public List<Memberexamination> tijian(String name) {
        List<Memberexamination> list = memberexaminationMapper.cha(name);
        for (Memberexamination memberexamination : list) {
            Member member = memberMapper.cha(memberexamination.getMemberid());
            memberexamination.setMember(member);
            Setmeal setmeal = setmealMapper.cha(memberexamination.getSetmealid());
            memberexamination.setSetmeal(setmeal);
            Order order = orderMapper.cha(memberexamination.getMemberid());
            memberexamination.setOrder(order);
        }
        return list;
    }


}
